// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //店铺及对应商品
    shop:[{
      id:1,
      name:"商店一",
      selected: false,
      allse:false,//当前店铺全选
      goods: [{
        sid: 1,//店铺id
        gid: 1,//商品id
        gname: "店一商品一",
        gprice: 200,//价格
        gnum: 1, //商品数量
        selected: false, //选中状态
        image: '',
      }],
    },{
      id: 2,
      name: "商店二",
      selected: false,
      allse: false,
      goods: [{
        sid: 2,
        gid: 1,
        gname: "店二商品一",
        gprice: 300,
        gnum: 1,
        selected: false,
        image: '',
      }, {
        sid: 2,
        gid: 2,
        gname: "店二商品二",
        gprice: 400,
        gnum: 1,
        selected: false,
        image: '',
      }],
    }],
    //全选
    allse:false,
    //总价
    allprice:0,
  },
  //单品选择
  goodsc:function(e){
    var that = this;
    var titem = that.data.shop;
    var cgoods = e.currentTarget.dataset;
    //取消当前店铺全选
    titem[cgoods.bindex].selected = false;
    titem[cgoods.bindex].allse = false;
    that.setData({
      allse: false
    });
    //当前产品
    var this_goods = titem[cgoods.bindex].goods[cgoods.gindex];
    //选取和取消
    if (this_goods.selected){
      this_goods.selected = false;
      var allprice = cgoods.allprice;
      allprice -= (this_goods.gprice * this_goods.gnum);
      //总价
      that.setData({
        allprice: allprice
      });
    }else{
      this_goods.selected = true;
      var allprice = cgoods.allprice;
      allprice += (this_goods.gprice * this_goods.gnum);
      //总价
      that.setData({
        allprice: allprice
      });
    }
    //更新数据
    that.setData({
      shop: titem
    });
  },
  //店铺选择
  shopse:function(e){
    var that = this;
    var list = that.data.shop;
    var cshop = e.currentTarget.dataset;
    for(var i=0; i<list.length; i++){
      if (cshop.id == list[i].id){
        //取消页面全选
        that.setData({
          allse: false
        });
        //当前店铺全选和取消
        if (list[i].allse){
          list[i].allse = false;
          list[i].selected = false;
          var allprice = cshop.allprice;
          for (var s = 0; s < list[i].goods.length; s++) {
            var tgoods0 = list[i].goods[s];
            if (tgoods0.selected){
              tgoods0.selected = false;
              allprice -= (tgoods0.gprice * tgoods0.gnum);
            }
          }
          //总价
          that.setData({
            allprice: allprice
          });
        }else{
          list[i].allse = true;
          list[i].selected = true;
          var allprice = cshop.allprice;
          for (var s = 0; s < list[i].goods.length; s++) {
            var tgoods = list[i].goods[s];
            if (!tgoods.selected){
              tgoods.selected = true;
              allprice += (tgoods.gprice * tgoods.gnum);
            }
          }
          //总价
          that.setData({
            allprice: allprice
          });
        }
        
      }
    }
    //更新数据
    that.setData({
      shop:list
    });
  },
  //全选
  allcheck:function(e){
    var that = this;
    var list = that.data.shop;
    var lprice = e.currentTarget.dataset.allprice;
    if (that.data.allse){
      for (var l = 0; l < list.length; l++) {
        list[l].selected = false;
        for (var t = 0; t < list[l].goods.length; t++) {
          var allgood = list[l].goods[t];
          if (allgood.selected){
            allgood.selected = false;
            lprice -= (allgood.gprice * allgood.gnum);
          }
        }
      }
      //总价
      that.setData({
        allprice: lprice,
        allse: false,
        shop: list
      });
    }else{
      for (var l = 0; l < list.length; l++) {
        list[l].selected = true;
        for (var t = 0; t < list[l].goods.length; t++) {
          var allgood = list[l].goods[t];
          if (!allgood.selected){
            allgood.selected = true;
            lprice += (allgood.gprice * allgood.gnum);
          }
        }
      }
      //总价
      that.setData({
        allprice: lprice,
        allse: true,
        shop: list
      });
    }
    
  },
  //数量增加
  upnum:function(e){
    var that = this;
    var lprice = e.currentTarget.dataset;
    var list = that.data.shop;
    //选中当前产品
    var current = list[lprice.bindex].goods[lprice.gindex];
    //数字增加
    if (current.gnum >= 99){
      current.gnum=99;
    }else{
      current.gnum++;
    }
    //总价增加
    var total_price = lprice.allprice;
    if (current.selected){
      total_price += current.gprice;
    }else{
      total_price += (current.gprice * current.gnum);
    }
    current.selected = true;
    that.setData({
      allprice: total_price,
      shop: list
    });
  },
  //数量减少
  downum:function(e){
    var that = this;
    var lprice = e.currentTarget.dataset;
    var list = that.data.shop;
    var current = list[lprice.bindex].goods[lprice.gindex];
    
    //数量减少
    var zq = current.gnum;
    if (current.gnum <= 1) {
      current.gnum = 1;
    } else {
      current.gnum--;
    }
    //总价
    var total_price = lprice.allprice;
    if (current.selected) {
      if (zq>1){
        total_price -= current.gprice;
      }
    }else{
      total_price += (current.gprice * current.gnum);
    }
    
    //选中当前产品
    current.selected = true;
    that.setData({
      allprice: total_price,
      shop: list
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})